package com.example.wasaimovie.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wasaimovie.Mapper.OrderMapper;
import com.example.wasaimovie.Model.entity.Arrangement;
import com.example.wasaimovie.Model.entity.Film;
import com.example.wasaimovie.Model.entity.Order;
import com.example.wasaimovie.Model.entity.User;
import com.example.wasaimovie.Service.ArrangementService;
import com.example.wasaimovie.Service.FilmService;
import com.example.wasaimovie.Service.OrderService;
import com.example.wasaimovie.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Value("${movie.image.url.path}")
    String url;

    @Autowired
    OrderService orderService;

    @Autowired
    FilmService filmService;

    @Autowired
    ArrangementService arrangementService;

    @Autowired
    UserService userService;

    /**
     * 查询已经被预定的座位
     * @param id 场次id
     * @return 座位列表
     */
    @Override
    public List<String> getSeats(String id){
        QueryWrapper<Order> qw = new QueryWrapper<>();
        qw.eq("aid", id);
        qw.eq("status", 2);
        List<Order> list = orderService.list(qw);
        List<String> result = new ArrayList<>();
        for (Order order : list) {
            String[] seats = order.getSeats().split(",");
            for (String seat : seats) {
                result.add(seat);
            }
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> getOrderById(String uid) {
        List<Map<String, Object>> result = new ArrayList<>();
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("uid", uid);
        List<Order> list = orderService.list(orderQueryWrapper);
        for (Order order : list) {
            Map<String, Object> map = new HashMap<>();
            // 获取用户信息
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("id", uid);
            User user = userService.list(userQueryWrapper).get(0);

            // 获取场次信息
            QueryWrapper<Arrangement> arrangementQueryWrapper = new QueryWrapper<>();
            arrangementQueryWrapper.eq("id", order.getAid());
            Arrangement arrangement = arrangementService.list(arrangementQueryWrapper).get(0);

            //获取电影信息
            QueryWrapper<Film> filmQueryWrapper = new QueryWrapper<>();
            filmQueryWrapper.eq("id", arrangement.getFid());
            Film film = filmService.list(filmQueryWrapper).get(0);
            film.setCover(url + film.getCover());

            // 整合结果
            map.put("order", order);
            map.put("user", user);
            map.put("film", film);
            map.put("arrangement", arrangement);
            result.add(map);
        }
        return result;
    }

}
